/*
 * qtapi.h
 *
 *  Created on: May 18, 2013
 *      Author: root
 */
#ifndef QTAPI_H_
#define QTAPI_H_


#include <unistd.h>

///////////msg code def////////
#define MSG_FUTURE_MD 		100
#define MSG_FUTURE_ORDER 	101
#define MSG_FUTURE_TRADE 	102
#define MSG_FUTURE_BAR 		103
#define MSG_STOCK_MD 		104
#define MSG_STOCK_ORDER 	105
#define MSG_STOCK_TRADE 	106
#define MSG_STOCK_BAR 		107
#define MSG_TIMER 			108
#define MSG_SET_PARAM		109
#define MSG_CTRL_MSG		110
#define MSG_EXIT			111
#define MSG_FINSTRUMENT_SATATUS 	112
#define MSG_PAUSEBYINS		113
#define MSG_SET_GLOBAL_PARAM 115
#define MSG_SET_EXT_PARAM	 116
#define MSG_DEL_EXT_PARAM	 117

//////policy status def////////
#define STATUS_DELETING 	-1
#define STATUS_READY 		0
#define STATUS_RUNNING 		1
#define STATUS_PAUSED 		2

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <set>
#include "msgs.h"
#include "pthread.h"

using namespace std;

class IPolicy;
class IPolicyMap;
class IFutureTrader;
class IStockTrader;
class IMarketDataListener;

enum BarType{
	Second,
	Minute,
	Hour,
	Day
};

struct MarketDataField{
	char 	TradingDay[12];
	char 	ExchangeID[8];
	char	InstrumentID[16];
	double  OpenPrice;
	double  LastPrice;
	double	LastVolume;
	double	PreClosePrice;
	double	PreSettlementPrice;
	double  PreOpenInterest;
	double	AskPrice[10];
	double	BidPrice[10];
	double	AskVolume[10];
	double	BidVolume[10];
	double	HighestPrice;
	double	LowestPrice;
	double	OpenInterest;
	double	Volume;
	double	Ammount;
	char	UpdateTime[10];
	int		MilliSeconds;
	double	UpperLimitPrice;
	double	AveragePrice;
	double	LowerLimitPrice;
	double	IOPV;

	MarketDataField(){
		memset(this,0,sizeof(MarketDataField));
	}
};

struct OrderField{
	int		OrderID;
	char	InstrumentID[16];
	char	OpenClose;
	char	Direction;
	char    ExchangeID[12];	   //SHFE,CFFEX,DCE,CZCE,SSE,SZE;
	char	OrderSysID[32];		//exchange order id
	int		BrokerOrderSeq;
	char 	TradingDay[10];
	char	InsertTime[10];
	char    OrderStatus;	//
	double  Price;
	int		Volume;
	double  AvgFillPrice;
	int		FilledVolume;
	char	HedgeFlag;	//
	char	OrderType;	//
	OrderField* ChildOrder;
	OrderField* ParentOrder;
	IPolicy*	pData;	//IPolicy object that send this order;
	IFutureTrader* 	pFutureTrader;	//IFutureTrader Object that send this order;
	IStockTrader* 	pStockTrader;	//IStockTrader Object that send this order,null if this order is an future order;

	int ErrorID;
	double fCloseProfit;	//for testing policy;
	//customer fields;
	long	nCust1;
	long	nCust2;
	long	nCust3;
	long	nCust4;
	long	nCust5;
	double	fCust1;
	double	fCust2;
	double	fCust3;
	double	fCust4;
	double	fCust5;

	OrderField(){
		memset(this,0,sizeof(OrderField));
	}
};

struct TradeField{
	int		OrderID;
	char	InstrumentID[32];
	char	OpenClose;
	char	Direction;
	char    ExchangeID[12];
	char	OrderSysID[32];
	int		BrokerOrderID;
	char	TradeTime[10];
	char 	TradeID[32];
	double  Price;
	int		Volume;
	int 	VolumeTotal;
	char 	CurrentOrderStatus;

	TradeField(){
		memset(this,0,sizeof(TradeField));
	}
};

struct FutureInstrumentField{
	char InstrumentID[32];
	char ProductClass[8];
	int  VolumeMultiply;
	double 	PriceTick;
	char 	ExpireDate[12];
	double 	MarginRate;
	char 	ExchangeID[16];
};

struct SecBatchInputOrder{
	char 	InstrumentID[8];
	int  	Volume;
	double 	Price;
	OrderField* pRtnOrder;
};

/**
 * This class is used by policy server to create a index bar data;
 */
class IFutureIndex2{
public:
	IFutureIndex2(){}
	virtual ~IFutureIndex2(){}
	virtual const char * GetInstrumentID()=0;
	virtual const char * GetExchange()=0;
	virtual bool accept(MarketDataField* pSrc,MarketDataField* pTarget)=0;
};

//Bar object
class IKBar {
public:
	IKBar(){
		m_fCust1 =0;
		m_fCust2 =0;
		m_fCust3 =0;
		m_fCust4 =0;
		m_fCust5 =0;
	}

	int 	m_nIndex;
	char 	m_sExchangeID[8];
	char	m_sInstrumentID[30]; //
	char	m_sTradingDay[16];	//
	char	m_sBeginTime[12];	//
	char	m_sEndTime[12];	//
	int		m_nInterval;

	double	m_fOpen;	//
	double	m_fHighest;	//
	double	m_fLowest;	//
	double	m_fClose;	//
	double	m_fAverage;	//
	double	m_nVolume;	//
	double	m_fAmmount; //
	double	m_nPosition;	//
	double	m_nPositionDiff;//
	double	m_nStatus;//0 opened,not close,1 closed, -1 not open;

	///////customer fields that user can use;
	double  m_fCust1;
	double  m_fCust2;
	double  m_fCust3;
	double  m_fCust4;
	double  m_fCust5;
};

//Bar Line
class IKBarSeries {
public:
	IKBar		**m_ppData;
	int			m_nCount;
	double		*m_pClosePrice;
	double		*m_pOpenPrice;
	double		*m_pHighPrice;
	double		*m_pLowPrice;
	double		*m_pVolume;
	double		*m_pPosition;
	double		*m_pAvgPrice;
	double		*m_pAmmount;
	double		*m_pPositionDiff;
	int			*m_pDate;
	int			*m_pTime;

	virtual ~IKBarSeries(){}

	/**
	 *
	 */
	virtual const char * GetInstrumentID()=0;

	/**
	 *
	 */
	virtual BarType GetType()=0;

	/**
	 *
	 */
	virtual int		GetPeriod()=0;

	virtual void GetClosePrice(double **ppData,int* pCount)=0;

	virtual void GetOpenPrice(double **ppData,int* pCount)=0;

	virtual void GetHighPrice(double **ppData,int* pCount)=0;

	virtual void GetLowPrice(double **ppData,int* pCount)=0;

	virtual void GetAvgPrice(double **ppData,int* pCount)=0;

	virtual void GetAmmount(double **ppData,int* pCount)=0;

	virtual void GetVolume(double **ppData,int* pCount)=0;

	virtual void GetPosition(double **ppData,int* pCount)=0;

	virtual void GetPositionDiff(double **ppData,int* pCount)=0;

	virtual void GetDate(int **ppData,int* pCount)=0;

	virtual void GetTime(int **ppData,int* pCount)=0;

	virtual void GetPricesAndVolume(double **ppOpenPrices,double **ppHighPrices,double **ppLowPrices,double **ppClosePrices,double **ppVolume, int* pCount)=0;

	virtual const IKBar * GetTopBar()=0;

	virtual void GetBars(IKBar **ppBars,int * pCount)=0;

	virtual void GetBars2(IKBar*** ppBars,int * pCount)=0;

	virtual IKBar* GetBar(int nIndex)=0;

	/**
	 * read history kbars from database;
	 *
	 * @param pszFromDate bars after this date will be read in;
	 */
	virtual void ReadHistory(const char *pszFromDate = NULL)=0;

	/**
	 * read history kbars from database;
	 *
	 * @param N the last N bars will be read in;
	 */
	virtual void ReadHistoryLastN(int N)=0;

	/**
	 * execute a sql to the kbar database;
	 */
	virtual int ExecuteSQL(const char *pszSql)=0;
};


//Bar Line Collection;
class IBarSeriesCollection {
public:
	IBarSeriesCollection(){}

	virtual ~IBarSeriesCollection(){}

	virtual void RegisterIndex(IFutureIndex2* pIndex)=0;

	virtual IKBarSeries * CreateSeries(const char * pszInstrumentID,const char *ExchangeID,BarType barType,int nInterval,int volumeMultiply,int nTotalBars)=0;

	virtual void RemoveSeries(IKBarSeries *pBarSeries)=0;

	virtual void Clear()=0;
};

class IFutureMD{
protected:
	char m_cName[64];
	char m_cType[64];

public:
	IFutureMD();

	virtual ~IFutureMD();

	virtual void SetName(const char*pszName);
	virtual const char* GetName();

	virtual void SetType(const char*pszType);
	virtual const char* GetType();

	static IFutureMD* CreateInstance(const char * pszName,const char* ConfigFile,const char * Type=0);

	/**
	 * login to trade;
	 */
	virtual int		Login(const char* UserID,const char* Password,char* ErrorMsg,int* ErrorID)=0;

	virtual int		SubscribeMarketData(const char *InstrumentID)=0;

	virtual void	UnsubscribeMarketData(const char *InstrumentID)=0;

	virtual void	AddListener(IMarketDataListener* pListener)=0;

	virtual void	RemoveListener(IMarketDataListener* pListener)=0;

	virtual void 	Stop()=0;

	virtual void	Release()=0;
};

class IStockMD{
protected:
	char m_cName[64];

public:
	static IStockMD* CreateInstance(const char *pszName,const char* ConfigFile,const char * Type=0);

	IStockMD();

	virtual ~IStockMD();

	virtual void SetName(const char*pszName);
	virtual const char* GetName();

	/**
	 *login to trade;
	 */
	virtual int		Login(const char* UserID,const char* Password,char* ErrorMsg,int* ErrorID)=0;

	virtual int		SubscribeMarketData(const char *InstrumentID)=0;

	virtual int		UnsubscribeMarketData(const char *InstrumentID)=0;

	virtual int		SubscribeMarketData(char **InstrumentID,int nCount)=0;

	virtual int		UnsubscribeMarketData(char **InstrumentID,int nCount)=0;

	virtual void	AddListener(IMarketDataListener* pListener)=0;

	virtual void	RemoveListener(IMarketDataListener* pListener)=0;

	virtual MarketDataField* GetLastMD(const char *pszIns)=0;

	virtual void 	Stop()=0;

	virtual void	Release()=0;
};


class IMarketDataListener{
public:
	set<string> m_FIS;
	IFutureMD* m_pFutureMD;
	IStockMD* m_pStockMD;


public:
	IMarketDataListener(){
		m_pFutureMD = 0;
		m_pStockMD = 0;
	}

	virtual ~IMarketDataListener(){

	}

	virtual void OnSetFutureMD(IFutureMD* pMD){
		m_pFutureMD = pMD;
	}

	virtual void OnSetStockMD(IStockMD* pMD){
		m_pStockMD = pMD;
	}

	virtual void SubscribeMarketData(const char *pszIns){
		int nTried=0;
		if(m_pFutureMD){
			int nRet = m_pFutureMD->SubscribeMarketData(pszIns);
			while(nRet !=0 && nTried++<3){
				sleep(1);
				nRet = m_pFutureMD->SubscribeMarketData(pszIns);
			}
			m_FIS.insert(pszIns);
		}
	}

	virtual void OnFutureMarketData(MarketDataField* pMD){

	};

	virtual void OnStockMarketData(MarketDataField* pMD){

	};
};

class IFutureAccount{
private:
	char m_cName[128];

public:
	static IFutureAccount* CreateInstance(const char * Name,const char *ConfigFile,const char* Type=0);

	static IFutureAccount* Get(const char *Name);

	IFutureAccount();

	virtual ~IFutureAccount();

	const char* GetName();

	void SetName(const char *pszName);

	virtual const char* GetUser()=0;

	virtual const char* GetPassword()=0;

	/**
	 *login to trade;
	 */
	virtual int Login(const char* UserID,const char* Password,char* ErrorMsg,int* ErrorID)=0;

	virtual int Logout()=0;

	virtual IFutureTrader* CreateTrader(IPolicy* pPolicy,char cHedgeFlag='1',bool IsSHFE=false)=0;

	virtual void 	Stop()=0;

	virtual void Release()=0;
};

class IFutureTrader:public IMarketDataListener{
public:
	IFutureTrader();

	virtual ~IFutureTrader();

	virtual IFutureAccount* GetFutureAccount()=0;

	virtual void SetSHFE(bool bSHFE)=0;

	/**
	 *buy open
	 */
	virtual OrderField* Buy(const char* InstrumentID,int nVolume,double fPrice)=0;

	/**
	 *sell close
	 */
	virtual OrderField* Sell(const char* InstrumentID,int nVolume,double fPrice,int closeTd)=0;

	/**
	 *sell open
	 */
	virtual OrderField* Short(const char* InstrumentID,int nVolume,double fPrice)=0;

	/**
	 *buy close
	 */
	virtual OrderField* Cover(const char* InstrumentID,int nVolume,double fPrice,int closeTd)=0;

	/**
	 *cancel an order;
	 */
	virtual bool CancelOrder(OrderField* pOrder)=0;

	/**
	 *get closable long position;
	 *
	 */
	virtual void GetLongPos(const char *pszCode,int& nTotal,int& nClosable,int& nTodayClosable)=0;


	virtual int GetLongClosable(const char *pszCode);

	/**
	 *get closable long position;
	 *
	 */
	virtual void GetLongPos(const char *pszCode,int& nTotal,int& nClosable,int& nTodayClosable,double& fAvgOpenPrice)=0;

	/**
	 *get closable short position;
	 *
	 */
	virtual void GetShortPos(const char *pszCode,int& nTotal,int& nClosable,int& nTodayClosable)=0;


	virtual void GetShortPos(const char *pszCode,int& nTotal,int& nClosable,int& nTodayClosable,double& fAvgOpenPrice)=0;


	virtual int GetShortClosable(const char *pszCode);

	/**
	 *get max volume that can be opened;
	 *
	 */
	//virtual int GetMaxOpenVolume(const char* InstrumentID,char cBuySell,double fPrice)=0;

	/**
	 *register a timer associated with an Order;
	 *
	 */
	virtual void RegisterTimer(int milliSeconds,int nAction,OrderField* pOrder)=0;


	/**
	 * get a instrumment field;
	 *
	 * @pIns must be a valid pointer to InstrumentField with InstrumentID filled;
	 */
	virtual bool GetInstrument(FutureInstrumentField* pIns)=0;


	virtual void GetInstrument(const char* pszProductID,FutureInstrumentField** ppIns,int* pCount)=0;
	/**
	 *
	 */
	virtual void GetTradingAccountInfo(double* PreBalance,double* CloseProfit,double* PositionProfit,double* Commission,double* Balance,double* Available)=0;

	virtual void GetTradingAccountInfo2(double* PreBalance,double* CloseProfit,double* PositionProfit,double* Margin,double* Commission,double* Balance,double* Available)=0;

	virtual const char * GetTradingDay()=0;

	virtual void Release()=0;

	virtual void LogError(const char *pszError)=0;

	virtual void LogDebug(const char *pszError)=0;

	virtual void LogInfo(const char *pszError)=0;
};


class IStockAccount:public IMarketDataListener{
private:
	char m_cName[128];

public:
	static IStockAccount* CreateInstance(const char* Name,const char *ConfigFile,const char* Type=0);

	static IStockAccount* Get(const char *Name,const char* Type=0);

	IStockAccount();

	virtual ~IStockAccount();

	const char* GetName();

	void SetName(const char *pszName);

	/**
	 *login to trade;
	 */
	virtual int Login(const char* UserID,const char* Password,char* ErrorMsg,int* ErrorID)=0;

	virtual void Logout()=0;

	virtual IStockTrader* CreateTrader(IPolicy* pPolicy)=0;

	virtual void Stop()=0;

	virtual void	Release()=0;
};

class IStockTrader:public IMarketDataListener{
public:

	IStockTrader();

	virtual ~IStockTrader();


	/**
	 *check whether a stock is in trading;
	 *
	 *@param pszIns stock code;
	 *@return true if in trading,false otherwise;
	 */
	virtual bool IsTrading(const char *pszIns)=0;

	/**
	 * whether a stock is trading as T+0;
	 *
	 */
	virtual bool IsT0(const char* pszIns)=0;

	/**
	 * get the exchange of a instrument;
	 * 1-SSE
	 * 2-SZE
	 * 0-BOTH
	 */
	virtual int GetExchange(const char *pszIns)=0;

	/**
	 *buy open
	 */
	virtual OrderField* Buy(const char* InstrumentID,int nVolume,double fPrice)=0;

	/**
	 *sell close
	 */
	virtual OrderField* Sell(const char* InstrumentID,int nVolume,double fPrice)=0;


	/**
	 *buy open
	 */
	virtual void BuyBatch(SecBatchInputOrder* pInputs,int nCount)=0;

	/**
	 *sell close
	 */
	virtual void SellBatch(SecBatchInputOrder* pInputs,int nCount)=0;


	/**
	 * sell credit;
	 */
	virtual OrderField* Short(const char* InstrumentID,int nVolume,double fPrice)=0;


	/**
	 *etf
	 */
	virtual OrderField* PurchaseOF(const char* InstrumentID,int nVolume,double fPrice)=0;

	/**
	 *etf
	 */
	virtual OrderField* RedeemOF(const char* InstrumentID,int nVolume,double fPrice)=0;

	/**
	 *cancel an order;
	 */
	virtual bool CancelOrder(OrderField* pOrder)=0;

	/**
	 * get total position of an instrument;
	 */
	virtual void GetPosition(const char* InstrumentID,int& nTotal,int& nClosable)=0;


	virtual void GetPosition(const char* InstrumentID,int& nTotal,int& nClosable,double& fAvgOpenPrice)=0;

	/**
	 *get max volume that can be opened;
	 *
	 */
	//virtual int GetMaxOpenVolume(const char* InstrumentID,char cBuySell,double fPrice)=0;

	/**
	 *
	 */
	virtual void GetTradingAccountInfo(double* PreBalance,double* PositionProfit,double* MarketValue,double* Available,double* Balance)=0;


	/**
	 *register a timer associated with an Order;
	 *
	 */
	virtual void RegisterTimer(int milliSeconds,int nAction,OrderField* pOrder)=0;

	//virtual void

	virtual void	Release()=0;
};

/**
 * this class is used by the framework. policies should not use this class;
 */
class IPolicyChangeListener{
public:
	IPolicyChangeListener(){}
	virtual ~IPolicyChangeListener(){};

	virtual void OnPolicyChanged(IPolicy* pPolicy,int nLevel=0)=0;
};


/**
 * all policies must inherit from this class;
 */
class IPolicy:public IMarketDataListener
{
public:
	static char m_cTradingDay[16];
protected:
	//policy status;
	int 	m_nStatus;
	//policy name;
	char 	m_cName[128];

	//the implementation .so file;
	char 	m_cSO[128];
	bool 	m_bAutoStart;

	//framework will set this field;
	IBarSeriesCollection* m_pBarsCollection;

	map<string,map<string,string>*> m_ExtParams;
	map<string,set<string>*> m_ChangedExtParams;

	map<string,string> m_Params;
	CLock 		m_lockParams;
	MSGQueue 	m_msgQ;


	CSpinLock 	m_PolicyLock;

	//pthread variables used to create thread in which to run this policy;
	pthread_t 	m_pthread;
	pthread_attr_t m_thAttr;

	//framework will set following two fields;
	IPolicyChangeListener* m_pListener;
	IPolicyMap* m_pPolicyMap;

public:
	IPolicy();
	virtual ~IPolicy();

	void SetStatus(int status);

	int GetStatus();

	void SetPolicyMap(IPolicyMap* pMap);

	void SetName(const char *pszName);

	const char* GetName();

	void SetSOFile(const char *pszSOFile);

	const char* GetSOFile();

	void SetAutoStart(bool bAutoStart);

	bool IsAutoStart();

	void RemoveAttr(const char *pszName);

	void SetAttr(const char *pszName,int iValue,bool fireChanged=false);

	void SetAttr(const char *pszName,double fValue,bool fireChanged=false,int nDigits=6);

	void SetAttr(const char *pszName,const char *pszValue,bool fireChanged=false);

	void GetAttr(const char *pszName,char * pszBuf);

	int GetIntAttr(const char *pszName);

	double GetDoubleAttr(const char *pszName);

	void CopyAttrs(map<string,string>* targets);


	void SetExtAttr(const char *pszCode,const char *pszAttr,const char *pszValue);

	void SetExtAttr(const char *pszCode,const char *pszAttr,int value);

	void SetExtAttr(const char *pszCode,const char *pszAttr,double value);

	char * GetExtAttr(const char *pszCode,const char* pszAttr,char *pszValue);

	int GetIntExtAttr(const char* pszCode,const char* pszAttr);

	double GetDoubleExtAttr(const char* pszCode,const char* pszAttr);

	void CopyExtAttrs(map<string,map<string,string>*>* targets);

	void RemoveExtAttr(const char *pszCode);

	void RemoveExtAttr(const char *pszCode,const char *pszAttr);

	void CopyChangedExtAttrs(map<string,set<string>*>* pMap);

	void DelExtAttr(const char *pszCode);

	void SetPolicyListener(IPolicyChangeListener* pListener);

	void FirePolicyChanged(int nLevel=0);

	void AddMsg(long type);

	void AddMsg(long type,void * lParam);

	void AddMsg(long type,void * lParam,long value1);

	void AddMsg(long type,void * lParam,long value1,long value2);

	virtual void  AddMsg(long type,void * lParam,long value1,long value2,long value3);

	MSGQueue* GetMSGQueue();

	void Start();

	void Stop();

	void Pause();

	void Resume();

	void Lock(){
		m_PolicyLock.Lock();
	}

	void Unlock(){
		m_PolicyLock.Unlock();
	}

	virtual bool NeedMDWhenPaused();

	/**
	 * this method is invoked by the md thread directly.
	 * generally, not to override this method;
	 */
	virtual void OnFutureMarketData(MarketDataField* pMD);

	/**
	 * this method is invoked by the md thread directly.
	 * generally, not to override this method;
	 */
	virtual void OnStockMarketData(MarketDataField* pMD);

	//framework will call this method just once for a Policy object.
	//policy can perform some initilization work here;
	virtual void Init(){

	}

	//called by the frame work to decide if the policy will accpet this future marketdata;
	virtual bool AcceptFMD(const char *pszCode){
		return true;
	}

	//policy can overwrite this method to accept future marketdata,
	//and submit order in this function;
	virtual void OnNewFutureMD(MarketDataField* pMD){

	}

	//policy can overwrite this method to accept stock marketdata,
	//and submit order in this function;
	virtual void OnNewStockMD(MarketDataField* pMD){

	}

	//framework will call this method according settings in
	//policyserver.xml
	virtual void OnSetFutureTrader(const char *Alias,IFutureTrader* pTrader){

	}

	//framework will call this method according settings in
	//policyserver.xml
	virtual void OnSetStockTrader(const char *Alias,IStockTrader* pTrader){

	}

	//gernerally,policy should not overwrite this method;
	//if do, please call IPolicy::OnSetBarsCollection(pBarsCollection) first;
	virtual void OnSetBarsCollection(IBarSeriesCollection* pBarsCollection){
		m_pBarsCollection = pBarsCollection;
	}

	//called by the framework when a global parameter changed;
	virtual void OnGlobalParamChanged(const char * pszName,const char *pszValue){

	}

	//called by framework when a future order submited by this policy is changed;
	//if nLastTradedVolume==-1, then this orderr is canceled;
	virtual void OnFOrderChanged(OrderField* pOrder,int nLastTradedVolume){

	}

	virtual void OnFOrderChanged(OrderField* pOrder,int nLastTradedVolume,int nFilledVolume){
		OnFOrderChanged(pOrder,nLastTradedVolume);
	}

	virtual void OnFOrderChanged(OrderField* pOrder,int nLastTradedVolume,int nFilledVolume,char cOrderStatus){
		OnFOrderChanged(pOrder,nLastTradedVolume,nFilledVolume);
	}

	//called by framework when a future order submited by this policy is filled;
	virtual void OnFOrderFilled(OrderField* pOrder,TradeField* pTrade){

	}

	//called by the framework when a bar in a series is opened;
	virtual void OnFBarClosed(IKBar* pBar,IKBarSeries* pSeries){

	}

	//called by the framework when a bar in a series is closed;
	virtual void OnFBarOpened(IKBar* pBar,IKBarSeries* pSeries){

	}

	//called by the framework when an exchange status changed.
	virtual void OnFInstrumentStatusChanged(const char* pszExchangeID,const char *pszTime,char newStatus){

	}

	//called when a stock order submitted by this policy is changed;
	virtual void OnSOrderChanged(OrderField* pOrder,int nLastTradedVolume){

	}

	virtual void OnSOrderChanged(OrderField* pOrder,int nLastTradedVolume,int nFilledVolume){
		OnSOrderChanged(pOrder,nLastTradedVolume);
	}

	virtual void OnSOrderChanged(OrderField* pOrder,int nLastTradedVolume,int nFilledVolume,char cOrderStatus){
		OnSOrderChanged(pOrder,nLastTradedVolume,nFilledVolume);
	}

	//called when a stock order submitted by this policy is filled;
	virtual void OnSOrderFilled(OrderField* pOrder,TradeField* pTrade){

	}

	virtual void OnSBarOpened(IKBar* pBar,IKBarSeries* pSeries){

	}

	virtual void OnSBarClosed(IKBar* pBar,IKBarSeries* pSeries){

	}

	//called when a timer registered by this policy is trigged;
	virtual void OnTimer(OrderField* pOrder,long lData){

	}

	//called by the framework to set param of this policy;;
	virtual void OnSetParam(const char* Name,const char * Value){

	}

	//called by the framework to set param of this policy;;
	virtual void OnSetExtParam(const char* Code,const char *Attr,const char * Value){

	}


	//called by the framework to set param of this policy;;
	virtual void OnDelExtParam(const char* Code){

	}


	//called by the framework when a policy thread is started.
	//after this call,the method will accept new market data;
	virtual void OnStarted(){

	}

	//called by the framework when this policy(or thread) is stoped;
	virtual void OnStoped(bool willDelete=false){

	}

	//called by the framework when this policy object is paused;
	virtual void OnPaused(){

	}

	//called by the framework when this policy object is resumed;
	virtual void OnResumed(){

	}

	//framework will call this method to release this policy object;
	virtual void Release()=0;

	/**
	 * framework will called this pos to close holding position;
	 */
	virtual void ClosePos(){

	}

	virtual void OpenPos(){

	}

	//framework will call this method to verify wheteher policy parameters is correct;
	//return true if correct, false otherwise;
	//if false, policy can copy some error msg to cErrorMsg and this msg will be sent to clients;
	virtual bool Verify(char* cErrorMsg){
		return true;
	}
};


/**
 * framework will implement this class. and a instance of this class will be set into
 * IPolicy-derived objects.
 */
class IPolicyMap{
public:
	IPolicyMap(){

	}

	virtual ~IPolicyMap(){

	}

	virtual void SetChanged()=0;

	virtual IPolicy* GetPolicy(const char *pszName)=0;

	virtual void SetGlobalString(const char *pszName,const char* nValue)=0;

	virtual void SetGlobalInt(const char *pszName,int nValue)=0;

	virtual void SetGlobalDouble(const char *pszName,double nValue)=0;

	virtual void GetGlobalString(const char *pszName,char* nValue)=0;

	virtual int GetGlobalInt(const char *pszName)=0;

	virtual double GetGlobalDouble(const char *pszName)=0;
};

#endif /* QTAPI_H_ */
